﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;

namespace Imgur.Net.Test
{
    [TestFixture]
    public class ResponseExceptionTests
    {
        [Test]
        public void CreateInstanceIntBoolTest()
        {
            const int status = 100;
            const bool success = true;

            var instance = new ResponseException(status, success);

            Assert.IsNotNull(instance);
            Assert.AreEqual(status, instance.Status);
            Assert.AreEqual(success, instance.Success);
        }

        [Test]
        public void CreateInstanceStringIntBoolTest()
        {
            const int status = 100;
            const bool success = true;
            const string message = "An exception message";

            var instance = new ResponseException(message, status, success);

            Assert.IsNotNull(instance);
            Assert.AreEqual(message, instance.Message);
            Assert.AreEqual(status, instance.Status);
            Assert.AreEqual(success, instance.Success);
        }

        [Test]
        public void CreateInstanceStringExceptionIntBoolTest()
        {
            const int status = 100;
            const bool success = true;
            const string message = "An exception message";
            Exception inner = new Exception("inner exception");

            var instance = new ResponseException(message, inner, status, success);

            Assert.IsNotNull(instance);
            Assert.AreEqual(message, instance.Message);
            Assert.AreEqual(inner, instance.InnerException);
            Assert.AreEqual(status, instance.Status);
            Assert.AreEqual(success, instance.Success);
        }

        [Test]
        public void CreateInstanceSerializationTest()
        {
            const int status = 100;
            const bool success = true;
            const string message = "An exception message";

            var instance = new ResponseException(message, status, success);

            ResponseException afterDeserializationException;

            using (Stream s = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(s, instance);
                s.Position = 0; // Reset stream position
                afterDeserializationException = (ResponseException)formatter.Deserialize(s);
            }

            Assert.IsNotNull(afterDeserializationException);
            Assert.AreEqual(message, afterDeserializationException.Message);
            Assert.AreEqual(status, afterDeserializationException.Status);
            Assert.AreEqual(success, afterDeserializationException.Success);
        }
    }
}
